﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Fitbit;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Security.Credentials;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Diagnostics;
using System.ComponentModel;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace GreetMeApplication
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public static Authenticator Auth;
        const string FitbitVaultResource = "[FitbitApp] Credentials";
        
        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var consumerKey = (string)Application.Current.Resources["ConsumerKey"];
            var consumerSecret = (string)Application.Current.Resources["ConsumeSecret"];
            var requestTokenUrl = (string)Application.Current.Resources["RequestTokenUrl"];
            var accessTokenUrl = (string)Application.Current.Resources["AccessTokenUrl"];
            var authorizeUrl = (string)Application.Current.Resources["AuthorizeUrl"];

            Auth = new Authenticator(consumerKey, consumerSecret, requestTokenUrl, accessTokenUrl, authorizeUrl);
            var url = Auth.GetAuthUrlToken();
            Launcher.LaunchUriAsync(new Uri(url));
        }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                var credentials = Auth.GetAuthCredentialFromPin(TxtPinNum.Text.Trim());
                var vault = new PasswordVault();
                vault.Add(new PasswordCredential(FitbitVaultResource, credentials.AuthToken, credentials.AuthTokenSecret));

                var creds = vault.FindAllByResource(FitbitVaultResource).FirstOrDefault();
                var authToken = creds.UserName;
                var authTokenSecret = vault.Retrieve(FitbitVaultResource, authToken).Password;

                var fitbit = new FitbitClient((string)Application.Current.Resources["ConsumerKey"], (string)Application.Current.Resources["ConsumeSecret"], authToken, authTokenSecret);

                var d = new DateTime(2013,2,4);//This is just a randome date I selected
                var profile = fitbit.GetUserProfile();
                var activities = fitbit.GetDayActivitySummary(d);
                var msg = String.Format("Hello {0}, Your last weight was {1}, you had {2} steps on {3}", profile.FullName,
                              profile.Weight, activities.Steps,d);
                new Windows.UI.Popups.MessageDialog(msg).ShowAsync();
            }



    }
}
